<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>JSDoc: Source: shake/shake.js</title>

    <script src="scripts/prettify/prettify.js"> </script>
    <script src="scripts/prettify/lang-css.js"> </script>
    <!--[if lt IE 9]>
      <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
    <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
    <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>

<body>

<div id="main">

    <h1 class="page-title">Source: shake/shake.js</h1>

    



    
    <section>
        <article>
            <pre class="prettyprint source linenums"><code>/**
 * Class Shake
 * @class
 * @classdesc 九宫格翻纸牌组件逻辑部分
 * @author pfan
 * 
 * @example
 *  new Shake(this,{
 *    shakeThreshold: 70, //阈值
 *    callback: (idx, award) => {
 *      //结束回调， 参数对应宫格索引，对应奖项    
 *    }
 *  })
 */
class Shake {

  /**
   * @constructs Shake构造函数
   * @param  {Object} pageContext page路由指针
   * @param  {Object} opts      组件所需参数
   * @param  {Number} opts.shakeThreshold  频率阈值
   * @param  {Function} opts.callback    结束回调
   */      
  constructor (pageContext, opts) {
    this.page = pageContext
    this.shakeThreshold = opts.shakeThreshold || 80
    this.lastX = 0
    this.lastY = 0
    this.lastZ = 0
    this.lastUpdate = 0
    this.isStart = true
    this.endCallBack = opts.callback
    this.page.audioCtx = wx.createAudioContext('shakeAudio')
    this.start()
    
  }

  start () {
    let { isStart, shakeThreshold, lastX, lastY, lastZ, lastUpdate } = this
    wx.onAccelerometerChange((res) => {
      let curTime = new Date().getTime()
      if ((curTime - lastUpdate) > 100) {
        let curX = res.x
        let curY = res.y
        let curZ = res.z
        let speed = Math.abs(curX + curY + curZ - lastX - lastY - lastZ) / (curTime - lastUpdate) * 10000
        if(speed > shakeThreshold &amp;&amp; this.isStart){
          this.page.audioCtx.play()   
          this.update()
        }
        lastUpdate = curTime
        lastX = curX
        lastY = curY
        lastZ = curZ
      }
    })
  }

  update () {
    this.page.setData({
      anim: true
    })
    this.isStart = false
    setTimeout(() => {
      this.page.setData({
        anim: false
      })
      this.endCallBack &amp;&amp; this.endCallBack()
    }, 2000)

  }

  reset () {
  
  }

}


export default Shake
</code></pre>
        </article>
    </section>




</div>

<nav>
    <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Card.html">Card</a></li><li><a href="FruitMachine.html">FruitMachine</a></li><li><a href="Lock.html">Lock</a></li><li><a href="Machine.html">Machine</a></li><li><a href="Scratch.html">Scratch</a></li><li><a href="Shake.html">Shake</a></li><li><a href="Wheel.html">Wheel</a></li></ul><h3>Global</h3><ul><li><a href="global.html#getDis">getDis</a></li><li><a href="global.html#runAsync">runAsync</a></li></ul>
</nav>

<br class="clear">

<footer>
    Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Tue Sep 19 2017 21:36:13 GMT+0800 (HKT)
</footer>

<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>
